<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/9/21
 * Time: 12:39
 */
namespace Home\Service;

use Admin\Enums\AuthorType;
use Think\Model;

class ScoreService extends Model
{
    /**
     * 分页获取谱单列表
     * @param $page
     * @param $limit
     * @return bool
     */
    public function getScoreList($page, $limit, &$count)
    {
        $count = M('Score')->where(['is_delete' => 0])->count();
        if (!$count) {
            $this->error = '查无记录';
            return false;
        }
        $list = M('Score')->where(['is_delete' => 0])->order('create_time desc')->page($page, $limit)->select();
        $scoreIds = array_column($list, 'id');
        //获取谱单首图
        $imgService = new ImageScoreService();
        $imgScoreMap = $imgService->ScoreImg($scoreIds);

        foreach ($list as $key => $val) {
            $list[$key]['img_url'] = $imgScoreMap[$val['id']]['url'];
        }
        return $list;

    }

    /**
     * 获取热门推荐谱单
     */
    public function scoreHot($limit = '')
    {
        $map = array(
            'is_delete' => 0,
            'score_id' => array('neq', 0)
        );
        $list = M('ScoreHot')->where($map)->group('score_id')->order('is_top desc')->limit($limit)->select();
        $scoreIds = array_column($list, 'score_id');
        $map = array(
            'id' => array('in', $scoreIds),
        );
        $scoreList = M('Score')->where($map)->select();
        //2.获取谱单图片
        $imgService = new ImageScoreService();
        $imgScoreMap = $imgService->ScoreImg($scoreIds);
        foreach ($scoreList as $key => $val) {
            $scoreList[$key]['img_url'] = $imgScoreMap[$val['id']]['url'];
        }
        return $scoreList;
    }

    public function scoreInfo($scoreList)
    {
        $scoreIds = array_column($scoreList, 'score_id');
        if (empty($scoreIds)) {
            $scoreIds = array_column($scoreList, 'id');
        }

        //获取谱单首图
        $imgService = new ImageScoreService();
        $imgScoreMap = $imgService->ScoreImg($scoreIds);
        foreach ($scoreList as $key => $val) {
            $scoreList[$key]['img_url'] = $val['score_id'] ? $imgScoreMap[$val['score_id']]['url'] : $imgScoreMap[$val['id']]['url'];
            //获取创建者信息
            if($val['author_type'] == AuthorType::$ADMIN) {
                $userInfo = M('Member')->where(array('uid'=>$val['creator']))->find();
                $scoreList[$key]['creator_info']['nickname'] = $userInfo['nickname'];
            } else {
                $userInfo = M('User')->where(array('id'=>$val['creator']))->find();
                $scoreList[$key]['creator_info']['nickname'] = $userInfo['nickname'];
                $scoreList[$key]['creator_info']['avatar_url'] = $userInfo['avatar_url'];
            }
        }
        return $scoreList;
    }

    /**
     * 获取用户创建的谱单
     * @param $userId
     * @return bool
     */
    public function getScoreByUesrId($userId)
    {
        if (empty($userId) || !is_numeric($userId)) {
            return false;
        }
        $map = array(
            'creator' => $userId,
            'is_delete' => 0
        );
        $list = M('Score')->where($map)->select();
        $scoreIds = array_column($list, 'id');

        //获取谱单首图
        $imgService = new ImageScoreService();
        $imgScoreMap = $imgService->ScoreImg($scoreIds);

        foreach ($list as $key => $val) {
            $list[$key]['img_url'] = $imgScoreMap[$val['id']]['url'];
        }
        return $list;
    }

    /**
     * 获取用户收藏谱单
     * @param $userId
     * @return bool
     */
    public function getFavScoreByUid($userId)
    {
        if (empty($userId) || !is_numeric($userId)) {
            return false;
        }
        $map = array(
          'user_id' => $userId,
          'is_delete' => 0
        );
        $list = M('ScoreFavorite')->where($map)->select();
        $scoreIds = array_column($list, 'score_id');

        //获取谱单信息
        $scoreMap = array(
            'id' => array('in', $scoreIds)
        );
        $scoreList = M('score')->where($scoreMap)->select();

        //获取谱单首图
        $imgService = new ImageScoreService();
        $imgScoreMap = $imgService->ScoreImg($scoreIds);

        foreach ($scoreList as $key => $val) {
            $scoreList[$key]['img_url'] = $imgScoreMap[$val['id']]['url'];
        }
        return $scoreList;
    }


    /**
     * 新建谱单
     * @param $param
     * @return bool
     */
    public function add($param)
    {
        if (empty($param['name']) || empty($param['creator'])) {
            return false;
        }
        $model = M('score');
        $data = $model->create($param);
        if ($data) {
            $id = $model->add($param);
        }
        return $id;
    }
}